package com.tjrac.generalpermissions.domain;

import com.tjrac.filter.entity.Verification;
import com.tjrac.filter.entity.VerificationRole;

import java.util.List;

/**
 * 角色实体类
 *
 * @author ZeNing
 * @create 2020-11-22 13:25
 */
public class Role implements VerificationRole {

    private Integer id;    //角色id 主键 自增
    private String name;   //角色名 非空
    private String desc;   //角色描述
    List<Verification> permissions;   // 拥有的权限表

    public Role() {
    }

    public Role(Integer id) {
        this.id = id;
    }

    public Role(Integer id, String name, String desc, List<Verification> permissions) {
        this.id = id;
        this.name = name;
        this.desc = desc;
        this.permissions = permissions;
    }

    public Role(String name, String desc, List<Verification> permissions) {
        this.name = name;
        this.desc = desc;
        this.permissions = permissions;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public List<Verification> getPermissions() {
        return permissions;
    }

    public void setPermissions(List<Verification> permissions) {
        this.permissions = permissions;
    }

    @Override
    public boolean verification(Verification permission) {
        if (permissions != null) {
            for (Verification p : permissions) {
                if (p.equals(permission))
                    return true;
            }
        }
        return false;
    }

    @Override
    public boolean verification(String link) {
        if (permissions != null){
            for (Verification p : permissions){
                String vLink = p.getLink();
                if (vLink == null) continue;
                if (link.matches(vLink)){
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public String toString() {
        return "Role{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", desc='" + desc + '\'' +
                ", permissions=" + permissions +
                '}';
    }
}
